package com.lihongman.web;


import java.io.IOException;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.lihongman.dao.CompanyDao;
import com.lihongman.dao.DaoyouDao;
import com.lihongman.dao.MessageDao;
import com.lihongman.entity.Company;
import com.lihongman.entity.Daoyou;
import com.lihongman.entity.Message;
import com.lihongman.entity.User;
import com.lihongman.util.PKUtils;

public class MessageServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		
		MessageDao dao = new MessageDao();
		
		List<Message> list = dao.findAll();
		session.setAttribute("list", list);
		
		if(user.getJuese().equals("admin")){
			
			String task = request.getParameter("task");
			if("add".equals(task)){
				if(request.getMethod().equalsIgnoreCase("post")){
					save(request,response);
				}else{
					toSave(request,response);
				}
			}else if("edit".equals(task)){
				if(request.getMethod().equalsIgnoreCase("get")){
					toEdit(request,response);
				}
				else{
					edit(request,response);
				}
			}else if("del".equals(task)){
				del(request,response);
			}else if("sousuo".equals(task)){
				sousuo(request,response);
			}else if("excel".equals(task)){
				excel(request,response);
			}else if("".equals(task) || task == null){
				request.getRequestDispatcher("/WEB-INF/views/list.jsp").forward(request, response);
			}
		}else if(user.getJuese().equals("worker")){
			request.getRequestDispatcher("/WEB-INF/views/add.jsp").forward(request, response);
		}
	}

	private void excel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		MessageDao dao = new MessageDao();
		dao.excel(session.getAttribute("list"),request,response);
	}

	private void sousuo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String danwei = request.getParameter("company");
		String daoyou = request.getParameter("daoyou");
		String source = request.getParameter("source");
		String starttime = request.getParameter("starttime");
		String endtime = request.getParameter("endtime");
		String minmoney = request.getParameter("minmoney");
		String maxmoney = request.getParameter("maxmoney");
		
		MessageDao mdao = new MessageDao();
		List<Message> listmessage = mdao.searchAll(danwei,daoyou,source,starttime,endtime,minmoney,maxmoney);
		session.setAttribute("list", listmessage);
		request.getRequestDispatcher("/WEB-INF/views/list.jsp").forward(request, response);
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		if(user.getJuese().equals("admin")){
			String id = request.getParameter("id");
			
			MessageDao dao = new MessageDao();
			dao.del(id);
			
			response.sendRedirect("message.html");
		}else{
			response.sendError(403);
		}
		
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		if(user.getJuese().equals("admin")){
			Message message = new Message();
			String id = request.getParameter("id");
			message.setTime(request.getParameter("time"));
			message.setMoney(Float.parseFloat(request.getParameter("money")));
			message.setRenshu(Integer.parseInt(request.getParameter("renshu")));
			message.setSource(request.getParameter("source"));
			
			String danwei = request.getParameter("company");
			String daoyou = request.getParameter("daoyou");
			
			CompanyDao comdao = new CompanyDao();
			Company company = comdao.findByName(danwei);
			
			if(company == null){
				company = new Company();
				company.setId(PKUtils.getPk());
				company.setCompany(danwei);
				comdao.save(company);
			}
			
			DaoyouDao dao = new DaoyouDao();
			Daoyou dyou = dao.findByName(daoyou);
			
			if(dyou == null){
				dyou = new Daoyou();
				dyou.setId(PKUtils.getPk());
				dyou.setName(daoyou);
				dao.save(dyou);
			}
			
			MessageDao m = new MessageDao();
			message.setId(id);
			message.setCompany(company);
			message.setDaoyou(dyou);
			message.setDanjia(message.getMoney()/message.getRenshu());
			m.update(message);
			
			response.sendRedirect("message.html");
		}
		
	}

	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		if(user.getJuese().equals("admin")){
			String id = request.getParameter("id");
			MessageDao dao = new MessageDao();
			Message message = dao.findById(id);
			
			request.setAttribute("message", message);
			request.getRequestDispatcher("/WEB-INF/views/edit.jsp").forward(request, response);
		}else{
			response.sendError(403);
		}
		
	}

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		Message message = new Message();
		message.setTime(request.getParameter("time"));
		message.setMoney(Float.parseFloat(request.getParameter("money")));
		message.setRenshu(Integer.parseInt(request.getParameter("renshu")));
		message.setSource(request.getParameter("source"));
		
		String danwei = request.getParameter("company");
		String daoyou = request.getParameter("daoyou");
		
		CompanyDao com=new CompanyDao();
		Company company = com.findByName(danwei);
		
		if(company == null){
			company = new Company();
			company.setId(PKUtils.getPk());
			company.setCompany(danwei);
			com.save(company);
		}
		
		DaoyouDao dao = new DaoyouDao();
		Daoyou dyou = dao.findByName(daoyou);
		
		if(dyou == null){
			dyou = new Daoyou();
			dyou.setId(PKUtils.getPk());
			dyou.setName(daoyou);
			dao.save(dyou);
		}
		
		MessageDao m = new MessageDao();
		message.setId(PKUtils.getPk());
		message.setCompany(company);
		message.setDaoyou(dyou);
		message.setDanjia(message.getMoney()/message.getRenshu());
		m.save(message);
		
		response.sendRedirect("message.html");
	}

	private void toSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/add.jsp").forward(request, response);
		
	}

}
